package com.szh.shenzhihaodemo;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


import com.szh.shenzhihaodemo.Adapter.ManagerAdapter;
import com.szh.shenzhihaodemo.Bean.UserInfoBean;
import com.szh.shenzhihaodemo.Utils.DBOpenHelper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";
    EditText name, pwd, sex;
    Button sign, register;
    DBOpenHelper helper;
    SQLiteDatabase db;
    List<UserInfoBean> mDatas;
    private ManagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDatas=new ArrayList<>();
        initView();//初始化控件
        adapter = new ManagerAdapter(this, mDatas);
    }

    private void initView() {
        name = findViewById(R.id.et_user_name);
        pwd = findViewById(R.id.et_user_pwd);
        sex = findViewById(R.id.et_user_sex);
        sign = findViewById(R.id.btn_sign);
        register = findViewById(R.id.btn_register);
        sign.setOnClickListener(this);//设置点击事件
        register.setOnClickListener(this);//设置点击事件
        sex.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {//点击事件
        switch (v.getId()) {
            case R.id.btn_sign://账号，密码正确后....跳转到用户管理界面
                Intent intent = new Intent();
                helper = new DBOpenHelper(this);
                db = helper.getWritableDatabase();
                String nameEt = name.getText().toString();
                String pwdEt = pwd.getText().toString();
                String sexEt = sex.getText().toString();
                Cursor userEtcursor = db.query("user", null, "name=?", new String[]{nameEt}, null,
                        null, null);//根据用户输入的name查找数据库中的数据
                userEtcursor.moveToNext();
                String passwordEt = userEtcursor.getString(userEtcursor.getColumnIndex("password"));
                if (pwdEt.equals(passwordEt)) {//判断该用户输入的密码是否正确  如果正确，查询数据库中所有数据跳转到用户管理界面
                    Cursor cursor = db.query("user", null, null,
                            null, null, null, null);
//        读取cursor当中的数据
                    while (cursor.moveToNext()) {
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String password = cursor.getString(cursor.getColumnIndex("password"));
                        int id = cursor.getInt(cursor.getColumnIndex("_id"));
                        String sex = cursor.getString(cursor.getColumnIndex("sex"));
                        UserInfoBean bean = new UserInfoBean(id, name, password,sex);
                        mDatas.add(bean);
                    }
                    cursor.close();
                    intent.setClass(this, UserManagerActivity.class);
                    intent.putExtra("datas", (Serializable) mDatas);
                    intent.setClass(this,UserManagerActivity.class);
                    startActivity(intent);
                }else {//判断该用户输入的密码是否正确  如果不正确
                    Toast.makeText(this, "用户名或密码不正确！！", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.btn_register://获取用户输入信息添加到数据库....跳转到用户管理界面
                Intent intentShow = new Intent();
                helper = new DBOpenHelper(this);
                db = helper.getWritableDatabase();
                ContentValues values = new ContentValues();
               //获取用户输入的用户名密码
                String userPwd;
                String userName;
                String userSex;
                userName = name.getText().toString();
                userPwd = pwd.getText().toString();
                userSex = sex.getText().toString();
                Log.d(TAG, "userPwd: "+userPwd);
                values.put("name", userName);
                values.put("password", userPwd);
                values.put("sex",userSex);
                db.insert("user", null, values);//给user表插入数据
                Cursor cursor = db.query("user", null, null,
                        null, null, null, null);
//        读取cursor当中的数据
                while (cursor.moveToNext()) {
                    String name = cursor.getString(cursor.getColumnIndex("name"));
                    String password = cursor.getString(cursor.getColumnIndex("password"));
                    int id = cursor.getInt(cursor.getColumnIndex("_id"));
                    String sex1 = cursor.getString(cursor.getColumnIndex("sex"));
                    Log.d(TAG, "sex1: "+sex1);
                    UserInfoBean bean = new UserInfoBean(id, name, password,sex1);
                    mDatas.add(bean);
                }
                cursor.close();
                intentShow.setClass(this, UserManagerActivity.class);
                intentShow.putExtra("datas", (Serializable) mDatas);

                Log.d(TAG, "sex: "+2);
                startActivity(intentShow);
                break;
        }

    }
}
